<template>
	<view>
		<view class="address_box flex_Z" v-for="(item,index) in addressList" :key="item.id" @click="changeAddress(item)">
			<u-radio-group v-model="ids" placement="column"  @change="groupChange" shape="square" activeColor="#FFC300">
				<view class="addrdee_top flex_dq">
					<text class="f32 c3 fw7">{{item.name}}</text> 
					<text class="f32 c3 fw7">{{item.phone}}</text> 
					<text class="lables">{{item.tag==0?'家':item.tag==1?'公司':'学校'}}</text>
				</view>
				<view class="address_c flex_dq">
					<text class="f28 c3">{{item.full_location}}{{item.address}}</text>
				</view>
				<view class="address_b flex_ld">
					<view class="defaults flex_dq">
						<u-radio  :name="item.id" @change="changeRadio((item.id))"></u-radio>
						<text :class="ids==item.id?'active':''">{{item.id==ids?'已默认':'默认'}}</text>
					</view>
					<view class=" flex_dq">
						<text class="btn_xg" @click.stop="tipShow = true,id = item.id">删除</text>
						<text class="btn_xg" @click.stop="modifyAddress(item)">修改</text>
					</view>
				</view>
			</u-radio-group>
		</view>
		<view class="empty2"></view>
		
		<u-popup :show="tipShow" mode="center" @close="tipShow = false" closeable bgColor="transparent">
			<view class="prompt flex_ZC">
				<text class="f36">提示</text>
				<text class="prompt_tit">确定删除该地址吗？</text>
				<view class="prompt_ntm flex_ld_a">
					<text @click="tipShow = false">取消</text>
					<text @click="del()">确定</text>
				</view>
			</view>
		</u-popup>
		<view class="addAdress flex_c" @click="goAddress">
			<text>新增收货地址</text>
		</view>
		
		<!-- 暂无数据 / 加载更多 -->
		<view class="nodata" v-if="addressList.length == 0">
			<image :src="baseUrl + '/static/image/noData1.png'" mode="aspectFill"></image>
			<text class="nodata_tit">暂无相关数据</text>
		</view>
	</view>
</template>

<script>
	import {
		addresslist,editaddress,setDefault,
		deleteaddress
	} from "../../api/user.js";
	export default {
		data() {
			return {
				baseUrl: this.$imageBaseUrl,
				id:'',
				ids: '',
				tipShow:false,
				status:false,
				changeState:false,
				addressList: [],
			}
		},
		onLoad(e) {
			console.log(e);
			if(e){
				this.status = e.status|| false
				this.changeState = e.changeState || false
			}
			console.log(this.status);
			console.log(this.changeState);
		},
		onShow() {
			this.initList()
		},
		methods: {
			del(){
				deleteaddress({
					id:this.id
				}).then(res=>{
					if(res.code==1){
						this.tipShow=false
						this.initList()
					}
					uni.$u.toast(res.msg)
				})
			},
			groupChange(n){
				console.log(n)
			},
			initList() {
				addresslist({
					page_size:50
				}).then(res => {
					console.log('1111', res)
					if(res.code == 1){
						this.addressList = res.data.data
						this.addressList.forEach((item)=>{
							if(item.is_default){
								this.ids = item.id
								console.log(this.ids)
							}
						})
					}
				})
			},
			changeRadio(id){
				console.log(id);
				let data = {
					id:id,
				}
				setDefault(data).then(res => {
					console.log(res)
					if(res.code == 1){
						uni.$u.toast(res.msg);
					
					}
				})
			},
			changeAddress(e){
				console.log(e);
				if(this.status){
					uni.navigateBack()
					uni.setStorageSync("changeAddress",e)
				}
				if(this.changeState){
					uni.navigateBack()
					uni.setStorageSync("replaceAddress",e)
				}
			},

			goAddress(){
				uni.navigateTo({
					url:'/pageUser/myuser/addaddress?state=' + 'add'
				})
			},
			modifyAddress(item){
				uni.navigateTo({
					url:'/pageUser/myuser/addaddress?state=' + 'modify&id='+item.id
				})
			},
			isDelete() {
				let that = this;
				console.log(this.id);
				let data = {
					id:this.id
				}
				addressDelete(data).then(res => {
					console.log(res)
					this.tipShow = false
					if(res.code == 1){
						uni.$u.toast(res.msg);
						uni.removeStorageSync("changeAddress")
						setTimeout(function() {
							that.initList()
						}, 1500);
					}
				})
			}
		},
	}
</script>

<style scoped>
	.address_box {
		width: 690rpx;
		height: 222rpx;
		background: #FFFFFF;
		border-radius: 12rpx;
		margin: 30rpx auto;
	}

	.addrdee_top {
		padding: 28rpx 24rpx;
	}

	.addrdee_top>text {
		margin-right: 24rpx;
	}

	.lables {
		font-size: 26rpx;
		color: #ffffff;
		background: #65A3FF;
		padding: 4rpx 30rpx;
		border-radius: 40rpx;
	}

	.address_c {
		padding: 0 24rpx;
	}
	.address_c>text{
		text-overflow:ellipsis;
		display:-webkit-box;
		-webkit-box-orient:vertical; 
		-webkit-line-clamp:1; 
		overflow:hidden; 
	}

	.address_b {
		padding: 20rpx 24rpx 0;
	}

	.btn_xg {
		width: 94rpx;
		height: 46rpx;
		line-height: 46rpx;
		background: #f7f7f7;
		border-radius: 24rpx;
		font-size: 26rpx;
		color: #333333;
		text-align: center;
		margin-left: 24rpx;
	}

	.defaults {
		font-size: 28rpx;
		color: #999999;
	}

	.active {
		color: #FFC300 !important;
	}
	.addAdress{
		position: fixed;
		bottom: 50rpx;
		left: 50%;
		transform: translateX(-50%);
		width: 690rpx;
		height: 80rpx;
		background: linear-gradient( 90deg, #FA2222 2%, #FFC400 98%);
		border-radius: 40rpx;
		font-size: 32rpx;
		color: #ffffff;
	}
	.prompt {
		width: 600rpx;
		background: #ffffff;
		margin: 0 auto;
		border-radius: 20rpx;
		padding-top: 10rpx;
		padding-bottom: 30rpx;
	}
	
	.prompt>text {
		margin-top: 40rpx;
	}
	
	.prompt_tit {
		margin: 0 30rpx;
		font-size: 28rpx;
		color: #666666;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
		overflow: hidden;
	}
	
	.prompt_ntm {
		width: 100%;
		font-size: 30rpx;
		margin-top: 50rpx;
	}
	.prompt_ntm>text:nth-child(1){
		width: 200rpx;
		height: 72rpx;
		line-height: 72rpx;
		text-align: center;
		background: #F2F2F2;
		border-radius: 40rpx;
		color: #333333;
	}
	.prompt_ntm>text:nth-child(2){
		width: 200rpx;
		height: 72rpx;
		line-height: 72rpx;
		text-align: center;
		background: linear-gradient( 90deg, #FA2222 2%, #FFC400 98%);
		border-radius: 40rpx;
		color: #ffffff;
	}
	/* 暂无数据 */
	.nodata {
		width: 100%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin: 80rpx auto;
		border-radius: 0 0 16rpx 16rpx;
	}

	.nodata>image {
	width: 290rpx;
	height: 240rpx;
	}

	.nodata_tit {
		font-size: 28rpx;
		color: #666666;
		margin-top: 30rpx;
	}
	
</style>